package ext.tianma.doc.util;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * DCC报表查询接口
 * @author deng
 *
 */
@Controller
@RequestMapping({ "/DCCInquireStatementController" })
public class DCCInquireStatementController {
	
	/**
	 * DCC报表查询
	 * 
	 * @param docType 工艺文件文档小类
	 * @param repository 产品库
	 * @param docName  文档名称
	 * @param startdate  开始时间
	 * @param enddate  结束时间
	 * @param modulesDevelopedLand 研发地
	 * @param moduleOrigin 生产地
	 * @param controlDocumentNum  受控PDF图纸
	 * @param states 图纸状态
	 * @param response 
	 * @throws Exception
	 */
	@RequestMapping(value = "/DCCInquireStatement", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void DCCInquireStatement(
			@RequestParam(value = "docType", required = false) String docType,
			@RequestParam(value = "repository", required = false) String repository,
			@RequestParam(value = "docName", required = false) String docName,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "modulesDevelopedLand", required = false) String modulesDevelopedLand,
			@RequestParam(value = "moduleOrigin", required = false) String moduleOrigin,
			@RequestParam(value = "controlDocumentNum", required = false) String controlDocumentNum,
			@RequestParam(value = "states", required = false) String states,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager
				.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = DCCInquireStatementService
					.getStatementByDocTypeAndRepository(docType, repository, docName, startdate, enddate ,modulesDevelopedLand, moduleOrigin, controlDocumentNum,states);
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	/**
	 * 导出excel
	 * 
	 * @param docType 工艺文件文档小类
	 * @param repository 产品库
	 * @param docName  文档名称
	 * @param startdate  开始时间
	 * @param enddate  结束时间
	 * @param modulesDevelopedLand 研发地
	 * @param moduleOrigin 生产地
	 * @param controlDocumentNum  受控PDF图纸
	 * @param states 图纸状态
	 * @param response 
	 * @throws Exception
	 */
	@RequestMapping(value = "/DCCWriteExcel", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void DCCWriteExcel(
			@RequestParam(value = "docType", required = false) String docType,
			@RequestParam(value = "repository", required = false) String repository,
			@RequestParam(value = "docName", required = false) String docName,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "modulesDevelopedLand", required = false) String modulesDevelopedLand,
			@RequestParam(value = "moduleOrigin", required = false) String moduleOrigin,
			@RequestParam(value = "controlDocumentNum", required = false) String controlDocumentNum,
			@RequestParam(value = "states", required = false) String states,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager
				.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			JSONObject ob = new JSONObject();
			String result = DCCInquireStatementService
					.getStatementByDocTypeAndRepository(docType, repository, docName, startdate, enddate ,modulesDevelopedLand, moduleOrigin, controlDocumentNum, states);
			if (StringUtils.isNotEmpty(result)) {// 返回数据不能为空
				JSONObject obj = new JSONObject(result);
				JSONArray array = new JSONArray(obj.getString("data"));
				if(array.length()>0) {
					String path = DCCWriteExcel.ecportExcel(array);
					ob.put("path", path);
					String resultData = ob.toString();
					out.print(resultData);
				}
			}
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

}